#include "dlgfammemberedit.h"
#include "ui_dlgfammemberedit.h"

DlgFamMemberEdit::DlgFamMemberEdit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgFamMemberEdit)
{
    ui->setupUi(this);

    connect(ui->btn_ok, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->btn_cancel, SIGNAL(clicked()), this, SLOT(reject()));
}

DlgFamMemberEdit::~DlgFamMemberEdit()
{
    delete ui;
}


void DlgFamMemberEdit::data_to_form()
{
    if(mqh_data.isEmpty()) return;
    QStringList qstrl = mp_dbms->get_fam_relations();
    ui->combo_relation->addItems(qstrl);

    mqv_t_index = mqh_data.value(FLD_FAM_TINDEX);
    ui->label_tenant->setText(RUSTR("Наниматель: ") + mp_dbms->get_tenant_fio(mqv_t_index,true));
    if(mqh_data.size() < 2 ) return;

    ui->ledit_family->setText( mqh_data.value(FLD_FAM_FAM).toString());
    ui->ledit_name->setText( mqh_data.value(FLD_FAM_NAME).toString());
    ui->ledit_pname->setText(mqh_data.value(FLD_FAM_PNAME).toString());
    ui->dedit_dob->setDate(mqh_data.value(FLD_FAM_DOB).toDate());

    ui->combo_relation->setEditText(mqh_data.value(FLD_FAM_RELATION).toString());
}

void DlgFamMemberEdit::form_to_data()
{
    mqh_data[FLD_FAM_DOB] = ui->dedit_dob->date();
    mqh_data[FLD_FAM_FAM] = ui->ledit_family->text().toUpper().trimmed();
    mqh_data[FLD_FAM_PNAME] = ui->ledit_pname->text().toUpper().trimmed();
    mqh_data[FLD_FAM_NAME] = ui->ledit_name->text().toUpper().trimmed();
    mqh_data[FLD_FAM_RELATION] = ui->combo_relation->currentText().toUpper().trimmed();
}

void DlgFamMemberEdit::setup_connection()
{
    connect(ui->ledit_family, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_name, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_pname, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->dedit_dob, SIGNAL(dateChanged(QDate)), this, SLOT(data_changed()));
    connect(ui->combo_relation, SIGNAL(editTextChanged(QString)), this, SLOT(data_changed()));
}

void DlgFamMemberEdit::accept()
{

    if(ui->ledit_family->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указана фамилия.");
        return;
    }

    if(ui->ledit_name->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указано имя.");
        return;
    }

    if(ui->combo_relation->currentText().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указана степень родства.");
        return;
    }

    QDialog::accept();
}
